<?php

namespace app\admin\model;

use traits\model\SoftDelete;

class Base extends \think\Model
{
    use SoftDelete;

    protected $pk = 'id';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'datetime';
    protected $dateFormat         = 'Y-m-d H:i:s';


    // 定义字段名
    protected $createTime = 'fk_bdl_createtime';
    protected $updateTime = 'fk_bdl_modifytime';
    protected $deleteTime = 'fk_bdl_deletetime';



    //初始化
    protected static function init()
    {
        // 新增前
        self::beforeInsert(function ($row) {

            if (!empty(session('admin.id'))) {
                $row->fk_bdl_creatorid = session('admin.id');
            }


            if($row->name == 'bd_materialimage' || $row->name=='other_problemlist' || $row->name=='bd_user')
            {

                switch ($row->name) {
                    case 'bd_materialimage':
                        $mes        = Materialimage::order('id','desc')->limit(1)->find();
                        break;
                    case 'other_problemlist':
                        $mes        = Problemlist::order('id','desc')->limit(1)->find();
                        break;
                    case 'bd_user':
                        $mes        = Wxuser::order('id','desc')->limit(1)->find();
                        break;
                }

                if(empty($mes))
                {
                    $fnumber = "001";
                }else{
                    $fnumber    = getNextCode($mes['fnumber']);
                }
                $row->fnumber = $fnumber;
            }

        });


        //新增后
        self::afterInsert(function ($row)
        {
            $need = ['home_carouselchart'];

            //新增后需要更新fweight
            if(in_array($row->name, $need))
            {
                $pk = $row->getPk();
                $row->getQuery()->where($pk, $row[$pk])->update(['fk_bdl_weight' => $row[$pk]]);
            }

            delCache();
        });


        // 更改前
        self::beforeUpdate(function ($row) {
            if (!empty(session('admin.id'))) {
                $row->fk_bdl_fmodifierid = session('admin.id');
            }
            delCache();
        });


        // 删除后
        self::afterDelete(function ($row) {
            $pk = $row->getPk();
            $row->getQuery()->where($pk, $row[$pk])->update(['fk_bdl_isdelete' => 1]);
            delCache();
        });
    }


    //使用状态
    public function getfenableList()
    {
        return ["1" => "可用", "0" => "禁用"];
    }
    public function getFkBdlAllqtyAttr($value)
    {
        return floatval($value);
    }


    //组织
    public function forg()
    {
        return $this->belongsTo('BdOrg', 'forgid');
    }
    //数据中心
    public function datacenter()
    {
        return $this->belongsTo('BdDatacenter', 'fk_bdl_bd_datacenter','fnumber');
    }


    //创建人
    public function creator()
    {
        return $this->hasOne("Admin", 'id', 'fk_bdl_creatorid');
    }

    //修改人
    public function modifier()
    {
        return $this->hasOne("Admin", 'id', 'fk_bdl_fmodifierid');
    }

}